package com.yan.dao.impl;

import com.yan.bean.Resume;
import com.yan.dao.ResumeDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author WangYan
 * @description:
 * @date 2021/5/28 9:40
 */
@Repository("resumeDao")
public class ResumeDaoImpl implements ResumeDao {

    @Autowired
    private MongoTemplate mongoTemplate;

    public void insertResume(Resume resume) {
        //插入数据的时候 如果不指定 collection（表名） 默认实体类名首字母小写作为表名
        //        mongoTemplate.insert(resume);
        //插入数据的时候 指定表名（集合名）
        mongoTemplate.insert(resume, "lg_resume_datas");
    }

    public Resume findByName(String name) {
        Query query = new Query();
        query.addCriteria(Criteria.where("name").is(name));
        List<Resume> result = mongoTemplate.find(query, Resume.class, "lg_resume_datas");
        return result.isEmpty() ? null : result.get(0);
    }

    public List<Resume> findListByNameAndExpectSalary(String name, double expectSalary) {
        Query query = new Query();
        query.addCriteria(Criteria.where("name").is(name).and("expectSalary").is(expectSalary));
        List<Resume> result = mongoTemplate.find(query, Resume.class, "lg_resume_datas");
        return result;
    }
}
